package com.android.base.common.assist;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.android.base.common.Log;

import java.lang.reflect.Method;

/**
 * assist us in sensing state of the networks.
 *
 * need  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 *
 * 半夜了，研究了一下Android的Network方面，发现网上有些文章理解的是不对的。
 * 以下方法是我研究得出的结论和方法，如有误也感谢指出。
 *
 * @author MaTianyu
 *         2014-1-8上午 00:33:11
 */
public class Network {
	private static final String TAG = Network.class.getSimpleName();

	public enum NetType {
		None(1),
		Mobile(2),
		Wifi(4),
		Other(8);

		NetType(int value) {
			this.value = value;
		}

		public int value;
	}

	public enum NetWorkType {
		UnKnown(-1),
		Wifi(1),
		Net2G(2),
		Net3G(3),
		Net4G(4);

		NetWorkType(int value) {
			this.value = value;
		}

		public int value;
	}
	//
	//public enum NetSubType {
	//    None(1),
	//    Mobile(2),
	//    Wifi(4),
	//    Other(8);
	//
	//    NetType(int value) {
	//        this.value = value;
	//    }
	//
	//    public int value;
	//}

	/**
	 * 获取ConnectivityManager
	 */
	public static ConnectivityManager getConnectivityManager(Context context) {
		return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	/**
	 * 获取ConnectivityManager
	 */
	public static TelephonyManager getTelephonyManager(Context context) {
		return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	}

	/**
	 * 判断网络连接是否有效（此时可传输数据）。
	 *
	 * @return boolean 不管wifi，还是mobile net，只有当前在连接状态（可有效传输数据）才返回true,反之false。
	 */
	public static boolean isConnected(Context context) {
		NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
		return net != null && net.isConnected();
	}

	/**
	 * 判断有无网络正在连接中（查找网络、校验、获取IP等）。
	 *
	 * @return boolean 不管wifi，还是mobile net，只有当前在连接状态（可有效传输数据）才返回true,反之false。
	 */
	public static boolean isConnectedOrConnecting(Context context) {
		NetworkInfo[] nets = getConnectivityManager(context).getAllNetworkInfo();
		if (nets != null) {
			for (NetworkInfo net : nets) {
				if (net.isConnectedOrConnecting()) { return true; }
			}
		}
		return false;
	}

	public static NetType getConnectedType(Context context) {
		NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
		if (net != null) {
			switch (net.getType()) {
				case ConnectivityManager.TYPE_WIFI:
					return NetType.Wifi;
				case ConnectivityManager.TYPE_MOBILE:
					return NetType.Mobile;
				default:
					return NetType.Other;
			}
		}
		return NetType.None;
	}

	/**
	 * 是否存在有效的WIFI连接
	 */
	public static boolean isWifiConnected(Context context) {
		NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
		return net != null && net.getType() == ConnectivityManager.TYPE_WIFI && net.isConnected();
	}

	/**
	 * 是否存在有效的移动连接
	 *
	 * @return boolean
	 */
	public static boolean isMobileConnected(Context context) {
		NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
		return net != null && net.getType() == ConnectivityManager.TYPE_MOBILE && net.isConnected();
	}

	/**
	 * 检测网络是否为可用状态
	 */
	public static boolean isAvailable(Context context) {
		return isWifiAvailable(context) || (isMobileAvailable(context) && isMobileEnabled(context));
	}

	/**
	 * 判断是否有可用状态的Wifi，以下情况返回false：
	 * 1. 设备wifi开关关掉;
	 * 2. 已经打开飞行模式；
	 * 3. 设备所在区域没有信号覆盖；
	 * 4. 设备在漫游区域，且关闭了网络漫游。
	 *
	 * @return boolean wifi为可用状态（不一定成功连接，即Connected）即返回ture
	 */
	public static boolean isWifiAvailable(Context context) {
		NetworkInfo[] nets = getConnectivityManager(context).getAllNetworkInfo();
		if (nets != null) {
			for (NetworkInfo net : nets) {
				if (net.getType() == ConnectivityManager.TYPE_WIFI) { return net.isAvailable(); }
			}
		}
		return false;
	}

	/**
	 * 判断有无可用状态的移动网络，注意关掉设备移动网络直接不影响此函数。
	 * 也就是即使关掉移动网络，那么移动网络也可能是可用的(彩信等服务)，即返回true。
	 * 以下情况它是不可用的，将返回false：
	 * 1. 设备打开飞行模式；
	 * 2. 设备所在区域没有信号覆盖；
	 * 3. 设备在漫游区域，且关闭了网络漫游。
	 *
	 * @return boolean
	 */
	public static boolean isMobileAvailable(Context context) {
		NetworkInfo[] nets = getConnectivityManager(context).getAllNetworkInfo();
		if (nets != null) {
			for (NetworkInfo net : nets) {
				if (net.getType() == ConnectivityManager.TYPE_MOBILE) { return net.isAvailable(); }
			}
		}
		return false;
	}

	/**
	 * 设备是否打开移动网络开关
	 *
	 * @return boolean 打开移动网络返回true，反之false
	 */
	public static boolean isMobileEnabled(Context context) {
		try {
			Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
			getMobileDataEnabledMethod.setAccessible(true);
			return (Boolean) getMobileDataEnabledMethod.invoke(getConnectivityManager(context));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 反射失败，默认开启
		return true;
	}

	/**
	 * 打印当前各种网络状态
	 *
	 * @return boolean
	 */
	public static boolean printNetworkInfo(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo in = connectivity.getActiveNetworkInfo();
			Log.i(TAG, "-------------$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$-------------");
			Log.i(TAG, "getActiveNetworkInfo: " + in);
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					// if (info[i].getType() == ConnectivityManager.TYPE_WIFI) {
					Log.i(TAG, "NetworkInfo[" + i + "]isAvailable : " + info[i].isAvailable());
					Log.i(TAG, "NetworkInfo[" + i + "]isConnected : " + info[i].isConnected());
					Log.i(TAG, "NetworkInfo[" + i + "]isConnectedOrConnecting : " + info[i].isConnectedOrConnecting());
					Log.i(TAG, "NetworkInfo[" + i + "]: " + info[i]);
					// }
				}
				Log.i(TAG, "\n");
			} else {
				Log.i(TAG, "getAllNetworkInfo is null");
			}
		}
		return false;
	}

	/**
	 * get connected network type by {@link ConnectivityManager}
	 *
	 * such as WIFI, MOBILE, ETHERNET, BLUETOOTH, etc.
	 *
	 * @return {@link ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_MOBILE},
	 * {@link ConnectivityManager#TYPE_ETHERNET}...
	 */
	public static int getConnectedTypeINT(Context context) {
		NetworkInfo net = getConnectivityManager(context).getActiveNetworkInfo();
		if (net != null) {
			if (Log.isPrint) {
				Log.i(TAG, "NetworkInfo: " + net.toString());
			}
			return net.getType();
		}
		return -1;
	}

	/**
	 * get network type by {@link TelephonyManager}
	 *
	 * such as 2G, 3G, 4G, etc.
	 *
	 * @return {@link TelephonyManager#NETWORK_TYPE_CDMA}, {@link TelephonyManager#NETWORK_TYPE_GPRS},
	 * {@link TelephonyManager#NETWORK_TYPE_LTE}...
	 */
	public static int getTelNetworkTypeINT(Context context) {
		return getTelephonyManager(context).getNetworkType();
	}

	/**
	 * GPRS    2G(2.5) General Packet Radia Service 114kbps
	 * EDGE    2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
	 * UMTS    3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
	 * CDMA    2G 电信 Code Division Multiple Access 码分多址
	 * EVDO_0  3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
	 * EVDO_A  3G 1.8mbps - 3.1mbps 属于3G过渡，3.5G
	 * 1xRTT   2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
	 * HSDPA   3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
	 * HSUPA   3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
	 * HSPA    3G (分HSDPA,HSUPA) High Speed Packet Access
	 * IDEN    2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 （属于2G，来自维基百科）
	 * EVDO_B  3G EV-DO Rev.B 14.7Mbps 下行 3.5G
	 * LTE     4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡，升级版 LTE Advanced 才是4G
	 * EHRPD   3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
	 * HSPAP   3G HSPAP 比 HSDPA 快些
	 *
	 * @return {@link  NetWorkType}
	 */
	public static NetWorkType getNetworkType(Context context) {
		int type = getConnectedTypeINT(context);
		switch (type) {
			case ConnectivityManager.TYPE_WIFI:
				return NetWorkType.Wifi;
			case ConnectivityManager.TYPE_MOBILE:
			case ConnectivityManager.TYPE_MOBILE_DUN:
			case ConnectivityManager.TYPE_MOBILE_HIPRI:
			case ConnectivityManager.TYPE_MOBILE_MMS:
			case ConnectivityManager.TYPE_MOBILE_SUPL:
				int teleType = getTelephonyManager(context).getNetworkType();
				switch (teleType) {
					case TelephonyManager.NETWORK_TYPE_GPRS:
					case TelephonyManager.NETWORK_TYPE_EDGE:
					case TelephonyManager.NETWORK_TYPE_CDMA:
					case TelephonyManager.NETWORK_TYPE_1xRTT:
					case TelephonyManager.NETWORK_TYPE_IDEN:
						return NetWorkType.Net2G;
					case TelephonyManager.NETWORK_TYPE_UMTS:
					case TelephonyManager.NETWORK_TYPE_EVDO_0:
					case TelephonyManager.NETWORK_TYPE_EVDO_A:
					case TelephonyManager.NETWORK_TYPE_HSDPA:
					case TelephonyManager.NETWORK_TYPE_HSUPA:
					case TelephonyManager.NETWORK_TYPE_HSPA:
					case TelephonyManager.NETWORK_TYPE_EVDO_B:
					case TelephonyManager.NETWORK_TYPE_EHRPD:
					case TelephonyManager.NETWORK_TYPE_HSPAP:
						return NetWorkType.Net3G;
					case TelephonyManager.NETWORK_TYPE_LTE:
						return NetWorkType.Net4G;
					default:
						return NetWorkType.UnKnown;
				}
			default:
				return NetWorkType.UnKnown;
		}
	}
}